<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2018/9/1
 * Time: 10:16
 */

namespace app\common\base;

use app\common\utils\JUtil;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\Log;

class Http extends Handle
{
    public function render(\Exception $e)
    {
        $errJson = array(
            'msg' => $e->getMessage(),
            'code' => $e->getStatusCode(),
        );
        Log::record(json_encode($errJson));
        // 参数验证错误
        if ($e instanceof ValidateException) {
            return json($e->getError(), 422);
        }

        // 请求异常
        if ($e instanceof HttpException && request()->isAjax()) {
            return response($e->getMessage(), $e->getStatusCode());
        }


        //可以在此交由系统处理
        return json(JUtil::E('请求接口不存在'));
    }

}